package com.qs.eggs.view.recharge.fragment;


import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.qs.eggs.Constants;
import com.qs.eggs.R;
import com.qs.eggs.base.fragment.BaseFragment;
import com.qs.eggs.view.activity.PayMethodActivity;
import com.qs.eggs.view.ordermanager.activity.OrderManagerActivity;

import org.json.JSONArray;

import java.util.HashMap;

import okhttp3.Call;
import okhttp3.Response;

/**
 * A simple {@link Fragment} subclass.
 */
public class DanshiFragment extends BaseFragment {
    private TextView yue;
    private EditText kahao;
    private Button ok;
    private String balance;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_danshi;
    }

    @Override
    protected void initWidget(View root) {
        super.initWidget(root);
        yue=findView(R.id.tv_yue);
        kahao=findView(R.id.et_kahao);
        ok=findView(R.id.ok);
        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(kahao.getText().toString())){
                    Toast.makeText(getActivity(),"卡号不能为空",Toast.LENGTH_SHORT).show();
                }else {
                    new AlertDialog.Builder(getActivity())
                            .setTitle("蛋事卡充值")
                            .setMessage("确定为此账户充值吗")
                            .setPositiveButton("确定",
                                    new DialogInterface.OnClickListener() {

                                        @Override
                                        public void onClick(DialogInterface dialog, int which) {
                                            chongzhi();
                                            dialog.dismiss();
                                        }
                                    })
                            .setNegativeButton("取消", new DialogInterface.OnClickListener() {

                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    dialog.dismiss();
                                }
                            }).setCancelable(false).show();
                }
            }
        });
    }
    @Override
    protected void initData() {
        super.initData();
        getInfo();
    }
    //获得个人信息中的账户余额
    private void getInfo(){
        OkGo.post(Constants.URL_BASE + "personal?action=getPersonalInfo")
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        System.out.print("个人信息==" + s);
                        try {
                            JSONArray jsonArray = new JSONArray(s);
                            for (int i = 0; i < jsonArray.length(); i++) {
                                balance = jsonArray.getJSONObject(i).getString("balance");
                                yue.setText("¥"+balance+"");
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });

    }
    //卡号充值操作
    private void chongzhi(){
        HashMap<String,String> map=new HashMap<String, String>();
        map.put("cardno",kahao.getText().toString());
        Gson gson=new Gson();
        String str=gson.toJson(map);
        OkGo.post(Constants.URL_BASE+"recharge?action=cardRecharge")
                .params("data",str)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        System.out.print("卡号充值=="+s);
                        if (s.equals("\"true\"")){
                            Toast.makeText(getActivity(),"充值成功",Toast.LENGTH_SHORT).show();
                            getInfo();
                        }else{
                            new AlertDialog.Builder(getActivity())
                                    .setTitle("")
                                    .setMessage("此账户已充值或没有此卡号")
                                    .setPositiveButton("确定",
                                            new DialogInterface.OnClickListener() {
                                                @Override
                                                public void onClick(DialogInterface dialog, int which) {
                                                    dialog.dismiss();
                                                }
                                            }).show();
                        }
                    }
                });
    }
}
